package oop.main;


import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;




/**
 * 
 * @author Tommy Morgan
 *
 *This class will be used to display the GUI for creating a new Person
 *It will also be the class for Person Objects
 *
 */
public class Person implements ActionListener {

	public String firstName;
	public String lastName;
	public int age;
	public static Person p1;
	private String ssn;
	
	private static JFrame personFrame;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// This is where the frame is created
		
		personFrame = new JFrame("Person");
		
		JLabel lblFirstName = new JLabel("First Name:");
		final JTextField txtFirstName = new JTextField();
		
		JLabel lblLastName = new JLabel("Last Name:");
		final JTextField txtLastName = new JTextField();
		
		JLabel lblAge = new JLabel("Age:");
		final JTextField txtAge = new JTextField();
		
		JLabel lblSSN = new JLabel("SSN:");
		final JTextField txtSSN = new JTextField();
		
		personFrame.add(lblFirstName);
		personFrame.add(txtFirstName);
		personFrame.add(lblLastName);
		personFrame.add(txtLastName);
		personFrame.add(lblAge);
		personFrame.add(txtAge);
		personFrame.add(lblSSN);
		personFrame.add(txtSSN);
		JButton submit = new JButton("Submit");
		submit.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				int age = Integer.parseInt(txtAge.getText());
				boolean isAble = true;
				String ssn = txtSSN.getText();
				
				final String SSN = "\\d{3}-\\d{2}-\\d{4}";
				boolean validSSN = ssn.matches(SSN);
				if(!validSSN)
				{
					System.out.println("Not a valid SSN. You entered " + ssn);
					isAble = false;
				}
				
				if(txtLastName.getText().equals(""))
				{
					System.out.println("Last name invalid.");
					isAble = false;
				}
				if(txtFirstName.getText().equals(""))
				{
					System.out.println("First name invalid.");
					isAble = false;
				}
				
				if(age < 0 || age > 115)
				{

					System.out.println("Invalid age. You entered " + age);
					isAble = false;
				}
				if(isAble)
					p1 = new Person(txtFirstName.getText(), txtLastName.getText(), age, ssn);
				
				
				
			}
		});
		personFrame.add(submit);
		
		
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new Person());
		personFrame.add(cancel);
		personFrame.setVisible(true);
		personFrame.setLayout(new GridLayout(0,2));
		personFrame.setSize(500, 500);
	}
	
	public Person()
	{
		
	}
	
	public Person(String firstName, String lastName, int age, String ssn)
	{
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
		setSsn(ssn);
	}

	/**
	 * @return the ssn
	 */
	public String getSsn() {
		return ssn;
	}

	/**
	 * @param ssn the ssn to set
	 */
	public void setSsn(String ssn) {
		//TODO Make sure this is the right length fos a ssn
		this.ssn = ssn;
	}
	
	@Override 
	public boolean equals(Object person) {
		if (person.getClass() == Person.class ) {
			Person p = (Person)person;
			if (this.ssn == p.getSsn())
				return true;
			else 
				return false;
		}
		else
			return false;
	}
	
	@Override
	public String toString() {
		String personString;
		personString = "First Name: " + firstName + "\nLast Name: " + lastName + "\nSSN:" + getSsn();
		return personString;
	}
	
	@Override
	public int hashCode() {
		return ((String) getSsn()).hashCode();
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		personFrame.setVisible(false);
	}

}
